package com.zzyl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description DeviceDataServiceImpl
 * @Author lyh
 * @Date 2024-11-02
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());
        LocalDateTime startDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime());
        LocalDateTime endDate = LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime());
        deviceDataPageReqDto.setStartDate(startDate);
        deviceDataPageReqDto.setEndDate(endDate);
        Page<DeviceDataVo> page=deviceDataMapper.findByCondition(deviceDataPageReqDto);
        return PageResponse.of(page,DeviceDataVo.class);
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos=deviceDataMapper.queryDeviceDataListByDay(iotId,functionId,startDate,endDate);
        Map<String, Double> map=null;
        if(CollectionUtil.isNotEmpty(deviceDataGraphVos)){
            map = deviceDataGraphVos.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        }
        List<DeviceDataGraphVo> deviceDataGraphVos1 = DeviceDataGraphVo.dayInstance(startDate);
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVos1) {
            Double value = map.get(deviceDataGraphVo.getDateTime());
            if(value!=null){
                deviceDataGraphVo.setDataValue(value);
            }

        }
        return deviceDataGraphVos1;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> dataGraphVoList= deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId,startDate,endDate);
        Map<String, Double> map=null;
        if(CollectionUtil.isNotEmpty(dataGraphVoList)){
            map = dataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        }
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.weekInstance(startDate);
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVos) {
            Double value = map.get(deviceDataGraphVo.getDateTime());
            if(value!=null){
                deviceDataGraphVo.setDataValue(value);
            }
        }
        return deviceDataGraphVos;
    }
}
